<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class NewsletterPage {
	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		if (!has_access("NEWSLETTER") && !has_access("ADMIN"))
			errorpage(5);
	}

	/**
	 * appropriate page action interpretations
	 */
	function page_inc_execute_action($action) { }

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		global $CONFIG;
		$creator = new FormCreator();
		
		$page->content['title'] = "Nieuwsbriefjes sturen";

		if ($_POST['confirmed'] == "YES") {
			
			db_do_query($page, db_query_insert(array('date_sent', 'user_sending', 'subject', 'content', 'fromstring'), array('NOW(14)', $_SESSION['user_id'], db_sqlize($_POST['subject']), db_sqlize($_POST['content']), db_sqlize("{$_POST['from']} &lt;{$_POST['from_mail']}>")), "NEWSLETTER_ARCHIVE"));
			$nl_id = db_insert_id();

			$sql = db_query_select(array(array("user_id", "nick", "email")), array("MEMBER"), ($_POST['ignore_user_pref'] ? 1 : "pref_newsletter = 1"));
			$names = db_do_query($page, $sql);
			
			$numrecipients = 0;
			
			$output = "<p>Opgeslagen in database</p><p>Mail verstuurd naar: \n<ol>\n";
			
			while ($r = db_fetch_array($names)) {
				
				$content = "{$_POST['content']}\n\n\nindien je de nieuwsbrief niet meer wil ontvangen, klik dan onderstaande link\n{$CONFIG['base_url']}{$CONFIG['homedir']}/{$r['user_id']}.unsubscribe?code=" . md5("{$r['nick']}{$r['email']}UNSUBSCRIBE");
			
				if (mail("{$r['nick']} <{$r['email']}>", $_POST['subject'], $content, "Reply-To: {$_POST['from_mail']}\r\nReturn-Path: {$_POST['from_mail']}\r\nFrom: {$_POST['from']} <{$_POST['from_mail']}>")) {
					$output .= "<li>{$r['nick']} ({$r['email']})\n";
					$numrecipients++;
				} else {
					$output .= "<li>SENDING FAILED for {$r['nick']} ({$r['email']})\n";
				}
			}
			
			db_do_query($page, db_query_update(array('date_sent', 'recipients'), array('date_sent', $numrecipients), "NEWSLETTER_ARCHIVE", "newsletter_id = $nl_id"));
			
			endpage($output . "</ol>");

		} else if ($_POST['submitted']) {
			$fields = array();
			$fields[] = $creator->input_hidden("ignore_user_pref", $_POST["ignore_user_pref"] ? 1 : 0);
			$fields[] = $creator->input_field("Afzender email", "from_mail", $_POST["from_mail"], 0, "", "", 1);
			$fields[] = $creator->input_field("Afzender", "from", $_POST["from"], 0, "", "", 1);
			$fields[] = $creator->input_text("Inhoud mail", "content", stripslashes($_POST["content"]), 0, 0, "", 1);
			$fields[] = $creator->input_field("Onderwerp", "subject", stripslashes($_POST["subject"]), 0, "", "", 1);
			$fields[] = $creator->input_radio("&nbsp;", 'confirmed', array('YES', 'NO'), array('ja', 'nee'));
			$fields[] = $creator->info("Ben je zeker dat je deze nieuwsbrief wil versturen naar alle leden?" . ($_POST['ignore_user_pref'] ? ", inclusief wie volgens zijn of haar profiel geen nieuwsbrief wil ontvangen" : ", behalve wie in zijn of haar profiel heeft gekozen om geen nieuwsbrief te ontvangen") . "?");
			$page->content['form'] = $creator->create("confirmform", $fields);

		} else {
		
			$inputs = array();
			$inputs[] = $creator->input_checkbox("Negeer user settings voor het ontvangen van nieuwsbrieven (stuurt maw. naar iedereen).", "ignore_user_pref");
			$inputs[] = $creator->input_field("Afzender email", "from_mail", $_SESSION['member']['email']);
			$inputs[] = $creator->input_field("Afzender", "from", $CONFIG['mail_from']);
			$inputs[] = $creator->info("Gebruik geen html codes in de inhoud van de nieuwsbrief of de subject line, want die worden vaak niet of niet correct weergegeven in email clients of webmail clients");
			$inputs[] = $creator->input_text("Inhoud mail", "content", "blablabla");
			$inputs[] = $creator->input_field("Onderwerp", "subject", "Mandragon nieuwsbrief ###");
			
			$page->content['form'] = $creator->create("nlform", $inputs);
			
		}
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_form($page, $page->content['form']);
	}
}

?>